package com.biz.primus.app.rest.controller.member;

import com.biz.primus.app.rest.controller.base.RestBaseController;
import com.biz.primus.app.rest.feign.member.MemberFeignClient;
import com.biz.primus.base.support.rest.vo.JsonResult;
import com.biz.primus.base.global.GlobalParams;
import com.biz.primus.model.member.exception.UserExceptionResponse;
import com.biz.primus.model.member.exception.UserExceptionType;
import com.biz.primus.model.member.vo.MemberCouponCodeVo;
import com.biz.primus.model.member.vo.PageVo;
import com.biz.primus.model.member.vo.request.*;
import com.biz.primus.model.member.vo.response.MemberLoginResponseVo;
import com.biz.primus.model.member.vo.response.MemberRegisterResponseVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户模块Controller
 *
 * @author jun.liu
 * @usage
 * @reviewer
 * @since 2017年11月3日
 */
@RestController
@RequestMapping("/users")
@Api(value = "用户模块接口文档")
public class MemberController extends RestBaseController
{

    private final MemberFeignClient memberFeignClient;

    @Autowired
    public MemberController(MemberFeignClient memberFeignClient) {
        this.memberFeignClient = memberFeignClient;
    }

    /**
     * 账号注册
     */
    @ApiOperation(value = "账号注册", response = MemberRegisterResponseVo.class)
    @PostMapping("/register")
    @UserExceptionResponse({
            UserExceptionType.PARAMS_ERROR,UserExceptionType.MOBILE_NOT_EXIST,
            UserExceptionType.MOBILE_ERROR,UserExceptionType.EMAIL_ERROR,
            UserExceptionType.MOBILE_EXISTED,UserExceptionType.EMAIL_EXISTED,
            UserExceptionType.USER_ALREADY_EXIST,UserExceptionType.CHANNEL_CODE_NOT_EXIST,
            UserExceptionType.SMSCODE_NOT_EXIST,UserExceptionType.CHANNEL_NOT_SUPPORTED
    })
    public JsonResult register(@RequestBody MemberRegisterRequestVo reqVo)
    {
        return new JsonResult(memberFeignClient.register(reqVo));
    }

    /**
     * 密码登录
     */
    @ApiOperation(value = "密码登录", response = MemberLoginResponseVo.class)
    @PostMapping("/login")
    @UserExceptionResponse({
            UserExceptionType.PARAMS_ERROR,UserExceptionType.USERNAME_NOT_EXIST,
            UserExceptionType.PASSWORD_NOT_EXIST,UserExceptionType.USERNAME_OR_PASSWORD_ERROR,
            UserExceptionType.OLD_PASSWORD_ERROR
    })
    public JsonResult login(GlobalParams gp, @RequestBody MemberLoginRequestVo reqVo)
    {
        reqVo = buildRequest(gp, reqVo);
        return new JsonResult(memberFeignClient.login(reqVo, true));
    }

    /**
     * 使用验证码登录(快捷登录/注册)
     */
    @ApiOperation(value = "使用验证码登录(快捷登录/注册)", response = MemberLoginResponseVo.class)
    @PostMapping("/smsCodeLogin")
    @UserExceptionResponse({
            UserExceptionType.SMSCODE_ERROR,UserExceptionType.PARAMS_ERROR
    })
    public JsonResult smsCodeLogin(GlobalParams gp, @RequestBody MemberSmsCodeLoginRequestVo reqVo)
    {
        reqVo = buildRequest(gp, reqVo);
        return new JsonResult(memberFeignClient.smsCodeLogin(reqVo, true));
    }

    /**
     * 登出
     */
    @ApiOperation("登出")
    @PostMapping("/logout")
    public JsonResult logout(GlobalParams gp)
    {
        // 暂未添加处理逻辑
        return new JsonResult();
    }

    @ApiOperation(value = "自动登录", response = MemberLoginResponseVo.class)
    @PostMapping("/autologin")
    @UserExceptionResponse({
            UserExceptionType.PARAMS_ERROR,UserExceptionType.USER_NOT_EXIST
    })
    public JsonResult autologin(GlobalParams gp)
    {
        BaseRequestVo reqVo = buildRequest(gp, new BaseRequestVo());
        return new JsonResult(memberFeignClient.autologin(reqVo, true));
    }

    @ApiOperation(value = "修改密码")
    @PostMapping("/changepassword")
    @UserExceptionResponse({
            UserExceptionType.PARAMS_ERROR,UserExceptionType.MEMBERID_NULL,
            UserExceptionType.PASSWORD_NOT_EXIST,UserExceptionType.NEW_PASSWORD_REPEATED,
            UserExceptionType.USER_NOT_EXIST,UserExceptionType.OLD_PASSWORD_ERROR
    })
    public JsonResult changepassword(GlobalParams gp, @RequestBody MemberChangePasswordRequestVo reqVo)
    {
        reqVo = buildRequest(gp, reqVo);
        memberFeignClient.changePassword(reqVo);
        return new JsonResult();
    }

    @ApiOperation(value = "重置密码")
    @PostMapping("/resetpassword")
    @UserExceptionResponse({
            UserExceptionType.PARAMS_ERROR,UserExceptionType.MOBILE_NOT_EXIST,
            UserExceptionType.SMSCODE_NOT_EXIST,UserExceptionType.PASSWORD_NOT_EXIST,
            UserExceptionType.USER_NOT_EXIST,UserExceptionType.SMSCODE_ERROR
    })
    public JsonResult resetpassword(GlobalParams gp, @RequestBody MemberResetPasswordRequestVo reqVo)
    {
        reqVo = buildRequest(gp, reqVo);
        memberFeignClient.resetPassword(reqVo);
        return new JsonResult();
    }


    //********************************************
    //            个  人   信   息
    //********************************************

    @ApiOperation(value = "个人信息(返回数据和登录返回数据相同", response = MemberLoginResponseVo.class)
    @GetMapping("/info")
    @UserExceptionResponse({
            UserExceptionType.ID_ERROR,UserExceptionType.USER_NOT_EXIST
    })
    public JsonResult info(GlobalParams gp)
    {
        BaseRequestVo reqVo = buildRequest(gp, new BaseRequestVo());
        MemberLoginResponseVo respVo = memberFeignClient.getUserInfo(reqVo, true);
        return new JsonResult(respVo);
    }

    @ApiOperation(value = "更新个人信息(生日,性别,昵称")
    @PostMapping("/updateinfo")
    @UserExceptionResponse({
            UserExceptionType.PARAMS_ERROR,UserExceptionType.USER_NOT_EXIST
    })
    public JsonResult updateinfo(GlobalParams gp, @RequestBody UpdateMemberPersonalInfoRequestVo reqVo)
    {
        reqVo = buildRequest(gp, reqVo);
        memberFeignClient.updatePersonalInfo(reqVo);
        return new JsonResult();
    }

    @ApiOperation(value = "更新头像,客户端先请求oss后拿到图片所在的url，再请求后台接口保存头像url")
    @PostMapping("/updateimage")
    @UserExceptionResponse({
            UserExceptionType.PARAMS_ERROR,UserExceptionType.USER_NOT_EXIST
    })
    public JsonResult updateAvatar(GlobalParams gp, @RequestBody UpdateMemberAvatarRequestVo reqVo)
    {
        reqVo = buildRequest(gp, reqVo);
        memberFeignClient.updateAvatar(reqVo);
        return new JsonResult();
    }

    //********************************************
    //               足      迹
    //********************************************

    @ApiOperation(value = "查看个人足迹(浏览的商品记录)", response = PageVo.class)
    @GetMapping("/footprint")
    @UserExceptionResponse({
            UserExceptionType.MEMBERID_NULL
    })
    public JsonResult findFootprint(GlobalParams gp, @RequestBody BasePageableRequestVo reqVo)
    {
        reqVo = buildRequest(gp, reqVo);
        PageVo respVo = memberFeignClient.findFootprint(reqVo);
        return new JsonResult(respVo);
    }

    @ApiOperation(value = "清空足迹")
    @PostMapping("/footprint/clear")
    @UserExceptionResponse({
            UserExceptionType.MEMBERID_NULL,UserExceptionType.FOOTPRINT_NULL
    })
    public JsonResult clearFootprint(GlobalParams gp)
    {
        BaseRequestVo reqVo = buildRequest(gp,new BaseRequestVo());
        memberFeignClient.clearFootprint(reqVo);
        return new JsonResult();
    }

    //********************************************
    //               优  惠   券
    //********************************************

    @ApiOperation(value = "个人中心优惠券", response = PageVo.class)
    @GetMapping("/coupon/list")
    public JsonResult couponList(GlobalParams gp, @RequestBody MemberCouponRequestVo reqVo)
    {
        reqVo = buildRequest(gp, reqVo);
        PageVo respVo = memberFeignClient.couponList(reqVo);
        return new JsonResult(respVo);
    }

    @ApiOperation(value = "优惠券详情", response = MemberCouponCodeVo.class)
    @GetMapping("/coupon/detail")
    public JsonResult getCouponDetail(GlobalParams gp, @RequestBody MemberCouponCodeRequestVo reqVo)
    {
        reqVo = buildRequest(gp, reqVo);
        MemberCouponCodeVo respVo = memberFeignClient.getCouponDetail(reqVo);
        return new JsonResult(respVo);
    }
}

